########################################################################################################################################################
# May 01 2019
# Replication File
# Rebecca Cordell, K. Chad Clay, Christopher J. Fariss, Reed M. Wood, Thorin M. Wright
# Changing Standards or Political Whim? Evaluating Changes in the Content of US State Department Human Rights Reports Following Presidential Transitions
# Journal of Human Rights
# https://www.tandfonline.com/toc/cjhr20/current 
########################################################################################################################################################

# Clear Workspace
rm(list=ls()) 

# Required Packages
require("ggpubr")
require("plyr")
library("ggplot2")
options(scipen=999)

# Load Data 
reports<-read.csv("reports.csv", stringsAsFactors=FALSE)

##############################################################################################################################################
# Figure 1: Percentage change in average word count of State Department human rights reports, during presidential administration transitions
##############################################################################################################################################

# Calculate total number of words per report year
ave_words<-setNames(aggregate(reports$total.words, by=list(Category=reports$year), FUN=sum), c("year", "words"))

# Calculate number of reports per year
ave_words$reports<-summary(as.factor(reports$year))

# Calculate average word count per report year
ave_words$ave.words<-ave_words$words/ave_words$reports

# Calculate difference between average word count for the first year of the new administration and the last year of the prior administration
ave_words$ave.diff<-NA
ave_words$ave.diff[1:2]<-ave_words[2,c(4)]-ave_words[1,c(4)]
ave_words$ave.diff[3:4]<-ave_words[4,c(4)]-ave_words[3,c(4)]
ave_words$ave.diff[5:6]<-ave_words[6,c(4)]-ave_words[5,c(4)]
ave_words$ave.diff[7:8]<-ave_words[8,c(4)]-ave_words[7,c(4)]
ave_words$ave.diff[9:10]<-ave_words[10,c(4)]-ave_words[9,c(4)]
ave_words$ave.diff[11:12]<-ave_words[12,c(4)]-ave_words[11,c(4)]

# Calculate percentage change in average word count for the first year of the new administration and the last year of the prior administration
ave_words$pct.change<-NA
ave_words$pct.change[1:2]<-ave_words[2,c(5)]/ave_words[1,c(4)]*100
ave_words$pct.change[3:4]<-ave_words[4,c(5)]/ave_words[3,c(4)]*100
ave_words$pct.change[5:6]<-ave_words[6,c(5)]/ave_words[5,c(4)]*100
ave_words$pct.change[7:8]<-ave_words[8,c(5)]/ave_words[7,c(4)]*100
ave_words$pct.change[9:10]<-ave_words[10,c(5)]/ave_words[9,c(4)]*100
ave_words$pct.change[11:12]<-ave_words[12,c(5)]/ave_words[11,c(4)]*100

# Perfrom Welch two sample t-test to determine whether difference in word count from the first year of the new administration and the last year of the prior administration is statistically significant at the 95% confidence level 
words_1980_1981<-reports[reports$transition=="Carter to Reagan",] 
words_1988_1989<-reports[reports$transition=="Reagan to G H W Bush",] 
words_1992_1993<-reports[reports$transition=="G H W Bush to Clinton",] 
words_2000_2001<-reports[reports$transition=="Clinton to G W Bush",]
words_2008_2009<-reports[reports$transition=="G W Bush to Obama",]
words_2016_2017<-reports[reports$transition=="Obama to Trump",]
ave_words$ttest.pvalue<-NA
ave_words$ttest.pvalue[1:2]<-t.test(total.words~year, data=words_1980_1981)$p.value
ave_words$ttest.pvalue[3:4]<-t.test(total.words~year, data=words_1988_1989)$p.value
ave_words$ttest.pvalue[5:6]<-t.test(total.words~year, data=words_1992_1993)$p.value
ave_words$ttest.pvalue[7:8]<-t.test(total.words~year, data=words_2000_2001)$p.value
ave_words$ttest.pvalue[9:10]<-t.test(total.words~year, data=words_2008_2009)$p.value
ave_words$ttest.pvalue[11:12]<-t.test(total.words~year, data=words_2016_2017)$p.value

# Produce graph
info<-reports[,c(5,7,8)]
ave_words<-join(ave_words,info, by ="year", match="first")
ave_words$transition<-factor(ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
figure1<-ggplot(ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(ave_words$transition)),
                   labels=c("Carter to Reagan"="Carter to Reagan", "Reagan to G H W Bush"="Reagan to G H W Bush", "G H W Bush to Clinton"="*G H W Bush to Clinton", "Clinton to G W Bush"="Clinton to G W Bush", "G W Bush to Obama"="G W Bush to Obama", "Obama to Trump"="*Obama to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
figure1

#####################################################################################################################################################
# Figure 2: Percentage change in section average word count of State Department human rights reports, during presidential administration transitions
#####################################################################################################################################################
# Calculate total number of words per section per report year
physical_words<-reports[,c(5,11)] 
civil_words<-reports[,c(5,12)] 
political_words<-reports[,c(5,13)]
groups_words<-reports[,c(5,14)] 
discrimination_words<-reports[,c(5,15)]
worker_words<-reports[,c(5,16)] 
physical_ave_words<-setNames(aggregate(physical_words$physical.words, by=list(Category=physical_words$year), FUN=sum), c("year", "words"))
civil_ave_words<-setNames(aggregate(civil_words$civil.words, by=list(Category=civil_words$year), FUN=sum), c("year", "words"))
political_ave_words<-setNames(aggregate(political_words$political.words, by=list(Category=political_words$year), FUN=sum), c("year", "words"))
groups_ave_words<-setNames(aggregate(groups_words$groups.words, by=list(Category=groups_words$year), FUN=sum), c("year", "words"))
discrimination_ave_words<-setNames(aggregate(discrimination_words$discrimination.words, by=list(Category=discrimination_words$year), FUN=sum), c("year", "words"))
worker_ave_words<-setNames(aggregate(worker_words$worker.words, by=list(Category=worker_words$year), FUN=sum), c("year", "words"))

# Calculate number of reports per section per year
physical_ave_words$reports<-summary(as.factor(reports$year))
civil_ave_words$reports<-summary(as.factor(reports$year))
political_ave_words$reports<-summary(as.factor(reports$year))
groups_ave_words$reports<-summary(as.factor(reports$year))
discrimination_ave_words$reports<-summary(as.factor(reports$year))
worker_ave_words$reports<-summary(as.factor(reports$year))

# Remove 1980 and 1981 from discrimination_ave_words and worker_ave_words as sections do not exist in reports
discrimination_ave_words<-discrimination_ave_words[!(discrimination_ave_words$year==1980 | discrimination_ave_words$year==1981),]
worker_ave_words<-worker_ave_words[!(worker_ave_words$year==1980 | worker_ave_words$year==1981),]

# Calculate average word count per section per report year
physical_ave_words$ave.words<-physical_ave_words$words/physical_ave_words$reports
civil_ave_words$ave.words<-civil_ave_words$words/civil_ave_words$reports
political_ave_words$ave.words<-political_ave_words$words/political_ave_words$reports
groups_ave_words$ave.words<-groups_ave_words$words/groups_ave_words$reports
discrimination_ave_words$ave.words<-discrimination_ave_words$words/discrimination_ave_words$reports
worker_ave_words$ave.words<-worker_ave_words$words/worker_ave_words$reports

# Calculate difference between average word count per section for the first year of the new administration and the last year of the prior administration
physical_ave_words$ave.diff<-NA
physical_ave_words$ave.diff[1:2]<-physical_ave_words[2,c(4)]-physical_ave_words[1,c(4)]
physical_ave_words$ave.diff[3:4]<-physical_ave_words[4,c(4)]-physical_ave_words[3,c(4)]
physical_ave_words$ave.diff[5:6]<-physical_ave_words[6,c(4)]-physical_ave_words[5,c(4)]
physical_ave_words$ave.diff[7:8]<-physical_ave_words[8,c(4)]-physical_ave_words[7,c(4)]
physical_ave_words$ave.diff[9:10]<-physical_ave_words[10,c(4)]-physical_ave_words[9,c(4)]
physical_ave_words$ave.diff[11:12]<-physical_ave_words[12,c(4)]-physical_ave_words[11,c(4)]
civil_ave_words$ave.diff<-NA
civil_ave_words$ave.diff[1:2]<-civil_ave_words[2,c(4)]-civil_ave_words[1,c(4)]
civil_ave_words$ave.diff[3:4]<-civil_ave_words[4,c(4)]-civil_ave_words[3,c(4)]
civil_ave_words$ave.diff[5:6]<-civil_ave_words[6,c(4)]-civil_ave_words[5,c(4)]
civil_ave_words$ave.diff[7:8]<-civil_ave_words[8,c(4)]-civil_ave_words[7,c(4)]
civil_ave_words$ave.diff[9:10]<-civil_ave_words[10,c(4)]-civil_ave_words[9,c(4)]
civil_ave_words$ave.diff[11:12]<-civil_ave_words[12,c(4)]-civil_ave_words[11,c(4)]
political_ave_words$ave.diff<-NA
political_ave_words$ave.diff[1:2]<-political_ave_words[2,c(4)]-political_ave_words[1,c(4)]
political_ave_words$ave.diff[3:4]<-political_ave_words[4,c(4)]-political_ave_words[3,c(4)]
political_ave_words$ave.diff[5:6]<-political_ave_words[6,c(4)]-political_ave_words[5,c(4)]
political_ave_words$ave.diff[7:8]<-political_ave_words[8,c(4)]-political_ave_words[7,c(4)]
political_ave_words$ave.diff[9:10]<-political_ave_words[10,c(4)]-political_ave_words[9,c(4)]
political_ave_words$ave.diff[11:12]<-political_ave_words[12,c(4)]-political_ave_words[11,c(4)]
groups_ave_words$ave.diff<-NA
groups_ave_words$ave.diff[1:2]<-groups_ave_words[2,c(4)]-groups_ave_words[1,c(4)]
groups_ave_words$ave.diff[3:4]<-groups_ave_words[4,c(4)]-groups_ave_words[3,c(4)]
groups_ave_words$ave.diff[5:6]<-groups_ave_words[6,c(4)]-groups_ave_words[5,c(4)]
groups_ave_words$ave.diff[7:8]<-groups_ave_words[8,c(4)]-groups_ave_words[7,c(4)]
groups_ave_words$ave.diff[9:10]<-groups_ave_words[10,c(4)]-groups_ave_words[9,c(4)]
groups_ave_words$ave.diff[11:12]<-groups_ave_words[12,c(4)]-groups_ave_words[11,c(4)]
discrimination_ave_words$ave.diff<-NA
discrimination_ave_words$ave.diff[1:2]<-discrimination_ave_words[2,c(4)]-discrimination_ave_words[1,c(4)]
discrimination_ave_words$ave.diff[3:4]<-discrimination_ave_words[4,c(4)]-discrimination_ave_words[3,c(4)]
discrimination_ave_words$ave.diff[5:6]<-discrimination_ave_words[6,c(4)]-discrimination_ave_words[5,c(4)]
discrimination_ave_words$ave.diff[7:8]<-discrimination_ave_words[8,c(4)]-discrimination_ave_words[7,c(4)]
discrimination_ave_words$ave.diff[9:10]<-discrimination_ave_words[10,c(4)]-discrimination_ave_words[9,c(4)]
worker_ave_words$ave.diff<-NA
worker_ave_words$ave.diff[1:2]<-worker_ave_words[2,c(4)]-worker_ave_words[1,c(4)]
worker_ave_words$ave.diff[3:4]<-worker_ave_words[4,c(4)]-worker_ave_words[3,c(4)]
worker_ave_words$ave.diff[5:6]<-worker_ave_words[6,c(4)]-worker_ave_words[5,c(4)]
worker_ave_words$ave.diff[7:8]<-worker_ave_words[8,c(4)]-worker_ave_words[7,c(4)]
worker_ave_words$ave.diff[9:10]<-worker_ave_words[10,c(4)]-worker_ave_words[9,c(4)]

# Calculate percentage change in average word count per section for the first year of the new administration and the last year of the prior administration
physical_ave_words$pct.change<-NA
physical_ave_words$pct.change[1:2]<-physical_ave_words[2,c(5)]/physical_ave_words[1,c(4)]*100
physical_ave_words$pct.change[3:4]<-physical_ave_words[4,c(5)]/physical_ave_words[3,c(4)]*100
physical_ave_words$pct.change[5:6]<-physical_ave_words[6,c(5)]/physical_ave_words[5,c(4)]*100
physical_ave_words$pct.change[7:8]<-physical_ave_words[8,c(5)]/physical_ave_words[7,c(4)]*100
physical_ave_words$pct.change[9:10]<-physical_ave_words[10,c(5)]/physical_ave_words[9,c(4)]*100
physical_ave_words$pct.change[11:12]<-physical_ave_words[12,c(5)]/physical_ave_words[11,c(4)]*100
civil_ave_words$pct.change<-NA
civil_ave_words$pct.change[1:2]<-civil_ave_words[2,c(5)]/civil_ave_words[1,c(4)]*100
civil_ave_words$pct.change[3:4]<-civil_ave_words[4,c(5)]/civil_ave_words[3,c(4)]*100
civil_ave_words$pct.change[5:6]<-civil_ave_words[6,c(5)]/civil_ave_words[5,c(4)]*100
civil_ave_words$pct.change[7:8]<-civil_ave_words[8,c(5)]/civil_ave_words[7,c(4)]*100
civil_ave_words$pct.change[9:10]<-civil_ave_words[10,c(5)]/civil_ave_words[9,c(4)]*100
civil_ave_words$pct.change[11:12]<-civil_ave_words[12,c(5)]/civil_ave_words[11,c(4)]*100
political_ave_words$pct.change<-NA
political_ave_words$pct.change[1:2]<-political_ave_words[2,c(5)]/political_ave_words[1,c(4)]*100
political_ave_words$pct.change[3:4]<-political_ave_words[4,c(5)]/political_ave_words[3,c(4)]*100
political_ave_words$pct.change[5:6]<-political_ave_words[6,c(5)]/political_ave_words[5,c(4)]*100
political_ave_words$pct.change[7:8]<-political_ave_words[8,c(5)]/political_ave_words[7,c(4)]*100
political_ave_words$pct.change[9:10]<-political_ave_words[10,c(5)]/political_ave_words[9,c(4)]*100
political_ave_words$pct.change[11:12]<-political_ave_words[12,c(5)]/political_ave_words[11,c(4)]*100
groups_ave_words$pct.change<-NA
groups_ave_words$pct.change[1:2]<-groups_ave_words[2,c(5)]/groups_ave_words[1,c(4)]*100
groups_ave_words$pct.change[3:4]<-groups_ave_words[4,c(5)]/groups_ave_words[3,c(4)]*100
groups_ave_words$pct.change[5:6]<-groups_ave_words[6,c(5)]/groups_ave_words[5,c(4)]*100
groups_ave_words$pct.change[7:8]<-groups_ave_words[8,c(5)]/groups_ave_words[7,c(4)]*100
groups_ave_words$pct.change[9:10]<-groups_ave_words[10,c(5)]/groups_ave_words[9,c(4)]*100
groups_ave_words$pct.change[11:12]<-groups_ave_words[12,c(5)]/groups_ave_words[11,c(4)]*100
discrimination_ave_words$pct.change<-NA
discrimination_ave_words$pct.change[1:2]<-discrimination_ave_words[2,c(5)]/discrimination_ave_words[1,c(4)]*100
discrimination_ave_words$pct.change[3:4]<-discrimination_ave_words[4,c(5)]/discrimination_ave_words[3,c(4)]*100
discrimination_ave_words$pct.change[5:6]<-discrimination_ave_words[6,c(5)]/discrimination_ave_words[5,c(4)]*100
discrimination_ave_words$pct.change[7:8]<-discrimination_ave_words[8,c(5)]/discrimination_ave_words[7,c(4)]*100
discrimination_ave_words$pct.change[9:10]<-discrimination_ave_words[10,c(5)]/discrimination_ave_words[9,c(4)]*100
worker_ave_words$pct.change<-NA
worker_ave_words$pct.change[1:2]<-worker_ave_words[2,c(5)]/worker_ave_words[1,c(4)]*100
worker_ave_words$pct.change[3:4]<-worker_ave_words[4,c(5)]/worker_ave_words[3,c(4)]*100
worker_ave_words$pct.change[5:6]<-worker_ave_words[6,c(5)]/worker_ave_words[5,c(4)]*100
worker_ave_words$pct.change[7:8]<-worker_ave_words[8,c(5)]/worker_ave_words[7,c(4)]*100
worker_ave_words$pct.change[9:10]<-worker_ave_words[10,c(5)]/worker_ave_words[9,c(4)]*100

# Perfrom Welch two sample t-test to determine whether difference in section word count from the first year of the new administration and the last year of the prior administration is statistically significant at the 95% confidence level 
physical_words_1980_1981<-physical_words[physical_words$year==1980 | physical_words$year==1981,] 
physical_words_1988_1989<-physical_words[physical_words$year==1988 | physical_words$year==1989,] 
physical_words_1992_1993<-physical_words[physical_words$year==1992 | physical_words$year==1993,] 
physical_words_2000_2001<-physical_words[physical_words$year==2000 | physical_words$year==2001,]
physical_words_2008_2009<-physical_words[physical_words$year==2008 | physical_words$year==2009,]
physical_words_2016_2017<-physical_words[physical_words$year==2016 | physical_words$year==2017,]
physical_ave_words$ttest.pvalue<-NA
physical_ave_words$ttest.pvalue[1:2]<-t.test(physical.words~year, data=physical_words_1980_1981)$p.value
physical_ave_words$ttest.pvalue[3:4]<-t.test(physical.words~year, data=physical_words_1988_1989)$p.value
physical_ave_words$ttest.pvalue[5:6]<-t.test(physical.words~year, data=physical_words_1992_1993)$p.value
physical_ave_words$ttest.pvalue[7:8]<-t.test(physical.words~year, data=physical_words_2000_2001)$p.value
physical_ave_words$ttest.pvalue[9:10]<-t.test(physical.words~year, data=physical_words_2008_2009)$p.value
physical_ave_words$ttest.pvalue[11:12]<-t.test(physical.words~year, data=physical_words_2016_2017)$p.value
civil_words_1980_1981<-civil_words[civil_words$year==1980 | civil_words$year==1981,] 
civil_words_1988_1989<-civil_words[civil_words$year==1988 | civil_words$year==1989,] 
civil_words_1992_1993<-civil_words[civil_words$year==1992 | civil_words$year==1993,] 
civil_words_2000_2001<-civil_words[civil_words$year==2000 | civil_words$year==2001,]
civil_words_2008_2009<-civil_words[civil_words$year==2008 | civil_words$year==2009,]
civil_words_2016_2017<-civil_words[civil_words$year==2016 | civil_words$year==2017,]
civil_ave_words$ttest.pvalue<-NA
civil_ave_words$ttest.pvalue[1:2]<-t.test(civil.words~year, data=civil_words_1980_1981)$p.value
civil_ave_words$ttest.pvalue[3:4]<-t.test(civil.words~year, data=civil_words_1988_1989)$p.value
civil_ave_words$ttest.pvalue[5:6]<-t.test(civil.words~year, data=civil_words_1992_1993)$p.value
civil_ave_words$ttest.pvalue[7:8]<-t.test(civil.words~year, data=civil_words_2000_2001)$p.value
civil_ave_words$ttest.pvalue[9:10]<-t.test(civil.words~year, data=civil_words_2008_2009)$p.value
civil_ave_words$ttest.pvalue[11:12]<-t.test(civil.words~year, data=civil_words_2016_2017)$p.value
political_words_1980_1981<-political_words[political_words$year==1980 | political_words$year==1981,] 
political_words_1988_1989<-political_words[political_words$year==1988 | political_words$year==1989,] 
political_words_1992_1993<-political_words[political_words$year==1992 | political_words$year==1993,] 
political_words_2000_2001<-political_words[political_words$year==2000 | political_words$year==2001,]
political_words_2008_2009<-political_words[political_words$year==2008 | political_words$year==2009,]
political_words_2016_2017<-political_words[political_words$year==2016 | political_words$year==2017,]
political_ave_words$ttest.pvalue<-NA
political_ave_words$ttest.pvalue[1:2]<-t.test(political.words~year, data=political_words_1980_1981)$p.value
political_ave_words$ttest.pvalue[3:4]<-t.test(political.words~year, data=political_words_1988_1989)$p.value
political_ave_words$ttest.pvalue[5:6]<-t.test(political.words~year, data=political_words_1992_1993)$p.value
political_ave_words$ttest.pvalue[7:8]<-t.test(political.words~year, data=political_words_2000_2001)$p.value
political_ave_words$ttest.pvalue[9:10]<-t.test(political.words~year, data=political_words_2008_2009)$p.value
political_ave_words$ttest.pvalue[11:12]<-t.test(political.words~year, data=political_words_2016_2017)$p.value
groups_words_1980_1981<-groups_words[groups_words$year==1980 | groups_words$year==1981,] 
groups_words_1988_1989<-groups_words[groups_words$year==1988 | groups_words$year==1989,] 
groups_words_1992_1993<-groups_words[groups_words$year==1992 | groups_words$year==1993,] 
groups_words_2000_2001<-groups_words[groups_words$year==2000 | groups_words$year==2001,]
groups_words_2008_2009<-groups_words[groups_words$year==2008 | groups_words$year==2009,]
groups_words_2016_2017<-groups_words[groups_words$year==2016 | groups_words$year==2017,]
groups_ave_words$ttest.pvalue<-NA
groups_ave_words$ttest.pvalue[1:2]<-t.test(groups.words~year, data=groups_words_1980_1981)$p.value
groups_ave_words$ttest.pvalue[3:4]<-t.test(groups.words~year, data=groups_words_1988_1989)$p.value
groups_ave_words$ttest.pvalue[5:6]<-t.test(groups.words~year, data=groups_words_1992_1993)$p.value
groups_ave_words$ttest.pvalue[7:8]<-t.test(groups.words~year, data=groups_words_2000_2001)$p.value
groups_ave_words$ttest.pvalue[9:10]<-t.test(groups.words~year, data=groups_words_2008_2009)$p.value
groups_ave_words$ttest.pvalue[11:12]<-t.test(groups.words~year, data=groups_words_2016_2017)$p.value
discrimination_words_1988_1989<-discrimination_words[discrimination_words$year==1988 | discrimination_words$year==1989,] 
discrimination_words_1992_1993<-discrimination_words[discrimination_words$year==1992 | discrimination_words$year==1993,] 
discrimination_words_2000_2001<-discrimination_words[discrimination_words$year==2000 | discrimination_words$year==2001,]
discrimination_words_2008_2009<-discrimination_words[discrimination_words$year==2008 | discrimination_words$year==2009,]
discrimination_words_2016_2017<-discrimination_words[discrimination_words$year==2016 | discrimination_words$year==2017,]
discrimination_ave_words$ttest.pvalue<-NA
discrimination_ave_words$ttest.pvalue[1:2]<-t.test(discrimination.words~year, data=discrimination_words_1988_1989)$p.value
discrimination_ave_words$ttest.pvalue[3:4]<-t.test(discrimination.words~year, data=discrimination_words_1992_1993)$p.value
discrimination_ave_words$ttest.pvalue[5:6]<-t.test(discrimination.words~year, data=discrimination_words_2000_2001)$p.value
discrimination_ave_words$ttest.pvalue[7:8]<-t.test(discrimination.words~year, data=discrimination_words_2008_2009)$p.value
discrimination_ave_words$ttest.pvalue[9:10]<-t.test(discrimination.words~year, data=discrimination_words_2016_2017)$p.value
worker_words_1988_1989<-worker_words[worker_words$year==1988 | worker_words$year==1989,] 
worker_words_1992_1993<-worker_words[worker_words$year==1992 | worker_words$year==1993,] 
worker_words_2000_2001<-worker_words[worker_words$year==2000 | worker_words$year==2001,]
worker_words_2008_2009<-worker_words[worker_words$year==2008 | worker_words$year==2009,]
worker_words_2016_2017<-worker_words[worker_words$year==2016 | worker_words$year==2017,]
worker_ave_words$ttest.pvalue<-NA
worker_ave_words$ttest.pvalue[1:2]<-t.test(worker.words~year, data=worker_words_1988_1989)$p.value
worker_ave_words$ttest.pvalue[3:4]<-t.test(worker.words~year, data=worker_words_1992_1993)$p.value
worker_ave_words$ttest.pvalue[5:6]<-t.test(worker.words~year, data=worker_words_2000_2001)$p.value
worker_ave_words$ttest.pvalue[7:8]<-t.test(worker.words~year, data=worker_words_2008_2009)$p.value
worker_ave_words$ttest.pvalue[9:10]<-t.test(worker.words~year, data=worker_words_2016_2017)$p.value

# Produce graph
physical_ave_words<-join(physical_ave_words,info, by ="year", match="first")
physical_ave_words$transition<-factor(physical_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
physical<-ggplot(physical_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(physical_ave_words$transition)))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
physical<-physical+ggtitle("Physical Integrity Rights") +theme(plot.title=element_text(hjust=0.5))
civil_ave_words<-join(civil_ave_words,info, by ="year", match="first")
civil_ave_words$transition<-factor(civil_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
civil<-ggplot(civil_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(civil_ave_words$transition)))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
civil<-civil+ggtitle("Civil Liberties") +theme(plot.title=element_text(hjust=0.5))
political_ave_words<-join(political_ave_words,info, by ="year", match="first")
political_ave_words$transition<-factor(political_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
political<-ggplot(political_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(political_ave_words$transition)),
                   labels=c("Carter to Reagan"="Carter to Reagan", "Reagan to G H W Bush"="*Reagan to G H W Bush", "G H W Bush to Clinton"="G H W Bush to Clinton", "Clinton to G W Bush"="Clinton to G W Bush", "G W Bush to Obama"="*G W Bush to Obama", "Obama to Trump"="*Obama to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
political<-political+ggtitle("Political Rights") +theme(plot.title=element_text(hjust=0.5))
groups_ave_words<-join(groups_ave_words,info, by ="year", match="first")
groups_ave_words$transition<-factor(groups_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
groups<-ggplot(groups_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(groups_ave_words$transition)))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
groups<-groups+ggtitle("Domestic and International Human Rights Groups") +theme(plot.title=element_text(hjust=0.5))
discrimination_ave_words<-join(discrimination_ave_words,info, by ="year", match="first")
discrimination_ave_words$transition<-factor(discrimination_ave_words$transition, levels=c("Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
discrimination<-ggplot(discrimination_ave_words[c(2,4,6,8,10),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(discrimination_ave_words$transition)),
                   labels=c("Reagan to G H W Bush"="*Reagan to G H W Bush", "G H W Bush to Clinton"="*G H W Bush to Clinton", "Clinton to G W Bush"="Clinton to G W Bush", "G W Bush to Obama"="*G W Bush to Obama", "Obama to Trump"="*Obama to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
discrimination<-discrimination+ggtitle("Discrimination and Societal Abuses") +theme(plot.title=element_text(hjust=0.5))
worker_ave_words<-join(worker_ave_words,info, by ="year", match="first")
worker_ave_words$transition<-factor(worker_ave_words$transition, levels=c("Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
worker<-ggplot(worker_ave_words[c(2,4,6,8,10),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(worker_ave_words$transition)),
                   labels=c("Reagan to G H W Bush"="Reagan to G H W Bush", "G H W Bush to Clinton"="*G H W Bush to Clinton", "Clinton to G W Bush"="Clinton to G W Bush", "G W Bush to Obama"="G W Bush to Obama", "Obama to Trump"="Obama to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
worker<-worker+ggtitle("Worker Rights") +theme(plot.title=element_text(hjust=0.5))
figure2<-ggarrange(physical, civil, political, groups, discrimination, worker, ncol=2, nrow=3, common.legend=TRUE, legend="bottom")
figure2

##############################################################################################################
# Figure 3: Percentage change in average word count of terms associated with women’s rights and LGBTI+ rights
##############################################################################################################

# Calculate total number of dictionary words per report year
family_words<-reports[,c(5,17)] 
gender_words<-reports[,c(5,18)] 
sexual_words<-reports[,c(5,19)]
vaw_words<-reports[,c(5,20)] 
lgbti_words<-reports[,c(5,21)]
family_ave_words<-setNames(aggregate(family_words$family.words, by=list(Category=family_words$year), FUN=sum), c("year", "words"))
gender_ave_words<-setNames(aggregate(gender_words$gender.words, by=list(Category=gender_words$year), FUN=sum), c("year", "words"))
sexual_ave_words<-setNames(aggregate(sexual_words$sexual.words, by=list(Category=sexual_words$year), FUN=sum), c("year", "words"))
vaw_ave_words<-setNames(aggregate(vaw_words$vaw.words, by=list(Category=vaw_words$year), FUN=sum), c("year", "words"))
lgbti_ave_words<-setNames(aggregate(lgbti_words$lgbti.words, by=list(Category=lgbti_words$year), FUN=sum), c("year", "words"))

# Calculate number of reports per year
family_ave_words$reports<-summary(as.factor(reports$year))
gender_ave_words$reports<-summary(as.factor(reports$year))
sexual_ave_words$reports<-summary(as.factor(reports$year))
vaw_ave_words$reports<-summary(as.factor(reports$year))
lgbti_ave_words$reports<-summary(as.factor(reports$year))

# Calculate average dictionary words per report year
family_ave_words$ave.words<-family_ave_words$words/family_ave_words$reports
gender_ave_words$ave.words<-gender_ave_words$words/gender_ave_words$reports
sexual_ave_words$ave.words<-sexual_ave_words$words/sexual_ave_words$reports
vaw_ave_words$ave.words<-vaw_ave_words$words/vaw_ave_words$reports
lgbti_ave_words$ave.words<-lgbti_ave_words$words/lgbti_ave_words$reports

# Calculate difference between average dictionary word count for the first year of the new administration and the last year of the prior administration
family_ave_words$ave.diff<-NA
family_ave_words$ave.diff[1:2]<-family_ave_words[2,c(4)]-family_ave_words[1,c(4)]
family_ave_words$ave.diff[3:4]<-family_ave_words[4,c(4)]-family_ave_words[3,c(4)]
family_ave_words$ave.diff[5:6]<-family_ave_words[6,c(4)]-family_ave_words[5,c(4)]
family_ave_words$ave.diff[7:8]<-family_ave_words[8,c(4)]-family_ave_words[7,c(4)]
family_ave_words$ave.diff[9:10]<-family_ave_words[10,c(4)]-family_ave_words[9,c(4)]
family_ave_words$ave.diff[11:12]<-family_ave_words[12,c(4)]-family_ave_words[11,c(4)]
gender_ave_words$ave.diff<-NA
gender_ave_words$ave.diff[1:2]<-gender_ave_words[2,c(4)]-gender_ave_words[1,c(4)]
gender_ave_words$ave.diff[3:4]<-gender_ave_words[4,c(4)]-gender_ave_words[3,c(4)]
gender_ave_words$ave.diff[5:6]<-gender_ave_words[6,c(4)]-gender_ave_words[5,c(4)]
gender_ave_words$ave.diff[7:8]<-gender_ave_words[8,c(4)]-gender_ave_words[7,c(4)]
gender_ave_words$ave.diff[9:10]<-gender_ave_words[10,c(4)]-gender_ave_words[9,c(4)]
gender_ave_words$ave.diff[11:12]<-gender_ave_words[12,c(4)]-gender_ave_words[11,c(4)]
sexual_ave_words$ave.diff<-NA
sexual_ave_words$ave.diff[1:2]<-sexual_ave_words[2,c(4)]-sexual_ave_words[1,c(4)]
sexual_ave_words$ave.diff[3:4]<-sexual_ave_words[4,c(4)]-sexual_ave_words[3,c(4)]
sexual_ave_words$ave.diff[5:6]<-sexual_ave_words[6,c(4)]-sexual_ave_words[5,c(4)]
sexual_ave_words$ave.diff[7:8]<-sexual_ave_words[8,c(4)]-sexual_ave_words[7,c(4)]
sexual_ave_words$ave.diff[9:10]<-sexual_ave_words[10,c(4)]-sexual_ave_words[9,c(4)]
sexual_ave_words$ave.diff[11:12]<-sexual_ave_words[12,c(4)]-sexual_ave_words[11,c(4)]
vaw_ave_words$ave.diff<-NA
vaw_ave_words$ave.diff[1:2]<-vaw_ave_words[2,c(4)]-vaw_ave_words[1,c(4)]
vaw_ave_words$ave.diff[3:4]<-vaw_ave_words[4,c(4)]-vaw_ave_words[3,c(4)]
vaw_ave_words$ave.diff[5:6]<-vaw_ave_words[6,c(4)]-vaw_ave_words[5,c(4)]
vaw_ave_words$ave.diff[7:8]<-vaw_ave_words[8,c(4)]-vaw_ave_words[7,c(4)]
vaw_ave_words$ave.diff[9:10]<-vaw_ave_words[10,c(4)]-vaw_ave_words[9,c(4)]
vaw_ave_words$ave.diff[11:12]<-vaw_ave_words[12,c(4)]-vaw_ave_words[11,c(4)]
lgbti_ave_words$ave.diff<-NA
lgbti_ave_words$ave.diff[1:2]<-lgbti_ave_words[2,c(4)]-lgbti_ave_words[1,c(4)]
lgbti_ave_words$ave.diff[3:4]<-lgbti_ave_words[4,c(4)]-lgbti_ave_words[3,c(4)]
lgbti_ave_words$ave.diff[5:6]<-lgbti_ave_words[6,c(4)]-lgbti_ave_words[5,c(4)]
lgbti_ave_words$ave.diff[7:8]<-lgbti_ave_words[8,c(4)]-lgbti_ave_words[7,c(4)]
lgbti_ave_words$ave.diff[9:10]<-lgbti_ave_words[10,c(4)]-lgbti_ave_words[9,c(4)]
lgbti_ave_words$ave.diff[11:12]<-lgbti_ave_words[12,c(4)]-lgbti_ave_words[11,c(4)]

# Calculate percentage change in average dictionary word count for the first year of the new administration and the last year of the prior administration
family_ave_words$pct.change<-NA
family_ave_words$pct.change[1:2]<-family_ave_words[2,c(5)]/family_ave_words[1,c(4)]*100
family_ave_words$pct.change[3:4]<-family_ave_words[4,c(5)]/family_ave_words[3,c(4)]*100
family_ave_words$pct.change[5:6]<-family_ave_words[6,c(5)]/family_ave_words[5,c(4)]*100
family_ave_words$pct.change[7:8]<-family_ave_words[8,c(5)]/family_ave_words[7,c(4)]*100
family_ave_words$pct.change[9:10]<-family_ave_words[10,c(5)]/family_ave_words[9,c(4)]*100
family_ave_words$pct.change[11:12]<-family_ave_words[12,c(5)]/family_ave_words[11,c(4)]*100
gender_ave_words$pct.change<-NA
gender_ave_words$pct.change[1:2]<-gender_ave_words[2,c(5)]/gender_ave_words[1,c(4)]*100
gender_ave_words$pct.change[3:4]<-gender_ave_words[4,c(5)]/gender_ave_words[3,c(4)]*100
gender_ave_words$pct.change[5:6]<-gender_ave_words[6,c(5)]/gender_ave_words[5,c(4)]*100
gender_ave_words$pct.change[7:8]<-gender_ave_words[8,c(5)]/gender_ave_words[7,c(4)]*100
gender_ave_words$pct.change[9:10]<-gender_ave_words[10,c(5)]/gender_ave_words[9,c(4)]*100
gender_ave_words$pct.change[11:12]<-gender_ave_words[12,c(5)]/gender_ave_words[11,c(4)]*100
sexual_ave_words$pct.change<-NA
sexual_ave_words$pct.change[1:2]<-sexual_ave_words[2,c(5)]/sexual_ave_words[1,c(4)]*100
sexual_ave_words$pct.change[3:4]<-sexual_ave_words[4,c(5)]/sexual_ave_words[3,c(4)]*100
sexual_ave_words$pct.change[5:6]<-sexual_ave_words[6,c(5)]/sexual_ave_words[5,c(4)]*100
sexual_ave_words$pct.change[7:8]<-sexual_ave_words[8,c(5)]/sexual_ave_words[7,c(4)]*100
sexual_ave_words$pct.change[9:10]<-sexual_ave_words[10,c(5)]/sexual_ave_words[9,c(4)]*100
sexual_ave_words$pct.change[11:12]<-sexual_ave_words[12,c(5)]/sexual_ave_words[11,c(4)]*100
vaw_ave_words$pct.change<-NA
vaw_ave_words$pct.change[1:2]<-vaw_ave_words[2,c(5)]/vaw_ave_words[1,c(4)]*100
vaw_ave_words$pct.change[3:4]<-vaw_ave_words[4,c(5)]/vaw_ave_words[3,c(4)]*100
vaw_ave_words$pct.change[5:6]<-vaw_ave_words[6,c(5)]/vaw_ave_words[5,c(4)]*100
vaw_ave_words$pct.change[7:8]<-vaw_ave_words[8,c(5)]/vaw_ave_words[7,c(4)]*100
vaw_ave_words$pct.change[9:10]<-vaw_ave_words[10,c(5)]/vaw_ave_words[9,c(4)]*100
vaw_ave_words$pct.change[11:12]<-vaw_ave_words[12,c(5)]/vaw_ave_words[11,c(4)]*100
lgbti_ave_words$pct.change<-NA
lgbti_ave_words$pct.change[1:2]<-lgbti_ave_words[2,c(5)]/lgbti_ave_words[1,c(4)]*100
lgbti_ave_words$pct.change[3:4]<-lgbti_ave_words[4,c(5)]/lgbti_ave_words[3,c(4)]*100
lgbti_ave_words$pct.change[5:6]<-lgbti_ave_words[6,c(5)]/lgbti_ave_words[5,c(4)]*100
lgbti_ave_words$pct.change[7:8]<-lgbti_ave_words[8,c(5)]/lgbti_ave_words[7,c(4)]*100
lgbti_ave_words$pct.change[9:10]<-lgbti_ave_words[10,c(5)]/lgbti_ave_words[9,c(4)]*100
lgbti_ave_words$pct.change[11:12]<-lgbti_ave_words[12,c(5)]/lgbti_ave_words[11,c(4)]*100

# Perfrom Welch two sample t-test to determine whether difference in dictionary word count from the first year of the new administration and the last year of the prior administration is statistically significant at the 95% confidence level 
family_words_1980_1981<-family_words[family_words$year==1980 | family_words$year==1981,] 
family_words_1988_1989<-family_words[family_words$year==1988 | family_words$year==1989,] 
family_words_1992_1993<-family_words[family_words$year==1992 | family_words$year==1993,] 
family_words_2000_2001<-family_words[family_words$year==2000 | family_words$year==2001,]
family_words_2008_2009<-family_words[family_words$year==2008 | family_words$year==2009,]
family_words_2016_2017<-family_words[family_words$year==2016 | family_words$year==2017,]
family_ave_words$ttest.pvalue<-NA
family_ave_words$ttest.pvalue[1:2]<-t.test(family.words~year, data=family_words_1980_1981)$p.value
family_ave_words$ttest.pvalue[3:4]<-t.test(family.words~year, data=family_words_1988_1989)$p.value
family_ave_words$ttest.pvalue[5:6]<-t.test(family.words~year, data=family_words_1992_1993)$p.value
family_ave_words$ttest.pvalue[7:8]<-t.test(family.words~year, data=family_words_2000_2001)$p.value
family_ave_words$ttest.pvalue[9:10]<-t.test(family.words~year, data=family_words_2008_2009)$p.value
family_ave_words$ttest.pvalue[11:12]<-t.test(family.words~year, data=family_words_2016_2017)$p.value
gender_words_1980_1981<-gender_words[gender_words$year==1980 | gender_words$year==1981,] 
gender_words_1988_1989<-gender_words[gender_words$year==1988 | gender_words$year==1989,] 
gender_words_1992_1993<-gender_words[gender_words$year==1992 | gender_words$year==1993,] 
gender_words_2000_2001<-gender_words[gender_words$year==2000 | gender_words$year==2001,]
gender_words_2008_2009<-gender_words[gender_words$year==2008 | gender_words$year==2009,]
gender_words_2016_2017<-gender_words[gender_words$year==2016 | gender_words$year==2017,]
gender_ave_words$ttest.pvalue<-NA
gender_ave_words$ttest.pvalue[1:2]<-t.test(gender.words~year, data=gender_words_1980_1981)$p.value
gender_ave_words$ttest.pvalue[3:4]<-t.test(gender.words~year, data=gender_words_1988_1989)$p.value
gender_ave_words$ttest.pvalue[5:6]<-t.test(gender.words~year, data=gender_words_1992_1993)$p.value
gender_ave_words$ttest.pvalue[7:8]<-t.test(gender.words~year, data=gender_words_2000_2001)$p.value
gender_ave_words$ttest.pvalue[9:10]<-t.test(gender.words~year, data=gender_words_2008_2009)$p.value
gender_ave_words$ttest.pvalue[11:12]<-t.test(gender.words~year, data=gender_words_2016_2017)$p.value
sexual_words_1980_1981<-sexual_words[sexual_words$year==1980 | sexual_words$year==1981,] 
sexual_words_1988_1989<-sexual_words[sexual_words$year==1988 | sexual_words$year==1989,] 
sexual_words_1992_1993<-sexual_words[sexual_words$year==1992 | sexual_words$year==1993,] 
sexual_words_2000_2001<-sexual_words[sexual_words$year==2000 | sexual_words$year==2001,]
sexual_words_2008_2009<-sexual_words[sexual_words$year==2008 | sexual_words$year==2009,]
sexual_words_2016_2017<-sexual_words[sexual_words$year==2016 | sexual_words$year==2017,]
sexual_ave_words$ttest.pvalue<-NA
sexual_ave_words$ttest.pvalue[1:2]<-t.test(sexual.words~year, data=sexual_words_1980_1981)$p.value
sexual_ave_words$ttest.pvalue[3:4]<-t.test(sexual.words~year, data=sexual_words_1988_1989)$p.value
sexual_ave_words$ttest.pvalue[5:6]<-t.test(sexual.words~year, data=sexual_words_1992_1993)$p.value
sexual_ave_words$ttest.pvalue[7:8]<-t.test(sexual.words~year, data=sexual_words_2000_2001)$p.value
sexual_ave_words$ttest.pvalue[9:10]<-t.test(sexual.words~year, data=sexual_words_2008_2009)$p.value
sexual_ave_words$ttest.pvalue[11:12]<-t.test(sexual.words~year, data=sexual_words_2016_2017)$p.value
vaw_words_1980_1981<-vaw_words[vaw_words$year==1980 | vaw_words$year==1981,] 
vaw_words_1988_1989<-vaw_words[vaw_words$year==1988 | vaw_words$year==1989,] 
vaw_words_1992_1993<-vaw_words[vaw_words$year==1992 | vaw_words$year==1993,] 
vaw_words_2000_2001<-vaw_words[vaw_words$year==2000 | vaw_words$year==2001,]
vaw_words_2008_2009<-vaw_words[vaw_words$year==2008 | vaw_words$year==2009,]
vaw_words_2016_2017<-vaw_words[vaw_words$year==2016 | vaw_words$year==2017,]
vaw_ave_words$ttest.pvalue<-NA
vaw_ave_words$ttest.pvalue[1:2]<-t.test(vaw.words~year, data=vaw_words_1980_1981)$p.value
vaw_ave_words$ttest.pvalue[3:4]<-t.test(vaw.words~year, data=vaw_words_1988_1989)$p.value
vaw_ave_words$ttest.pvalue[5:6]<-t.test(vaw.words~year, data=vaw_words_1992_1993)$p.value
vaw_ave_words$ttest.pvalue[7:8]<-t.test(vaw.words~year, data=vaw_words_2000_2001)$p.value
vaw_ave_words$ttest.pvalue[9:10]<-t.test(vaw.words~year, data=vaw_words_2008_2009)$p.value
vaw_ave_words$ttest.pvalue[11:12]<-t.test(vaw.words~year, data=vaw_words_2016_2017)$p.value
lgbti_words_1980_1981<-lgbti_words[lgbti_words$year==1980 | lgbti_words$year==1981,] 
lgbti_words_1988_1989<-lgbti_words[lgbti_words$year==1988 | lgbti_words$year==1989,] 
lgbti_words_1992_1993<-lgbti_words[lgbti_words$year==1992 | lgbti_words$year==1993,] 
lgbti_words_2000_2001<-lgbti_words[lgbti_words$year==2000 | lgbti_words$year==2001,]
lgbti_words_2008_2009<-lgbti_words[lgbti_words$year==2008 | lgbti_words$year==2009,]
lgbti_words_2016_2017<-lgbti_words[lgbti_words$year==2016 | lgbti_words$year==2017,]
lgbti_ave_words$ttest.pvalue<-NA
lgbti_ave_words$ttest.pvalue[1:2]<-t.test(lgbti.words~year, data=lgbti_words_1980_1981)$p.value
lgbti_ave_words$ttest.pvalue[3:4]<-t.test(lgbti.words~year, data=lgbti_words_1988_1989)$p.value
lgbti_ave_words$ttest.pvalue[5:6]<-t.test(lgbti.words~year, data=lgbti_words_1992_1993)$p.value
lgbti_ave_words$ttest.pvalue[7:8]<-t.test(lgbti.words~year, data=lgbti_words_2000_2001)$p.value
lgbti_ave_words$ttest.pvalue[9:10]<-t.test(lgbti.words~year, data=lgbti_words_2008_2009)$p.value
lgbti_ave_words$ttest.pvalue[11:12]<-t.test(lgbti.words~year, data=lgbti_words_2016_2017)$p.value

# Produce graph
family_ave_words<-join(family_ave_words,info, by ="year", match="first")
family_ave_words$transition<-factor(family_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
family<-ggplot(family_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(family_ave_words$transition)),
                   labels=c("Carter to Reagan"="Carter to Reagan", "Reagan to G H W Bush"="Reagan to G H W Bush", "G H W Bush to Clinton"="*G H W Bush to Clinton", "Clinton to G W Bush"="Clinton to G W Bush", "G W Bush to Obama"="G W Bush to Obama", "Obama to Trump"="*Obama to Trump")) +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
family<-family+ggtitle("Family") +theme(plot.title=element_text(hjust=0.5))
gender_ave_words<-join(gender_ave_words,info, by ="year", match="first")
gender_ave_words$transition<-factor(gender_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
gender<-ggplot(gender_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(gender_ave_words$transition)),
                   labels=c("Carter to Reagan"="*Carter to Reagan", "Reagan to G H W Bush"="Reagan to G H W Bush", "G H W Bush to Clinton"="*G H W Bush to Clinton", "Clinton to G W Bush"="*Clinton to G W Bush", "G W Bush to Obama"="*G W Bush to Obama", "Obama to Trump"="*Obama to Trump")) +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
gender<-gender+ggtitle("Gender") +theme(plot.title=element_text(hjust=0.5))
sexual_ave_words<-join(sexual_ave_words,info, by ="year", match="first")
sexual_ave_words$transition<-factor(sexual_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
sexual<-ggplot(sexual_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(sexual_ave_words$transition)),
                   labels=c("Carter to Reagan"="Carter to Reagan", "Reagan to G H W Bush"="Reagan to G H W Bush", "G H W Bush to Clinton"="G H W Bush to Clinton", "Clinton to G W Bush"="Clinton to G W Bush", "G W Bush to Obama"="*G W Bush to Obama", "Obama to Trump"="*Obama to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
sexual<-sexual+ggtitle("Sexual and Reproductive Rights") +theme(plot.title=element_text(hjust=0.5))
vaw_ave_words<-join(vaw_ave_words,info, by ="year", match="first")
vaw_ave_words$transition<-factor(vaw_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
vaw<-ggplot(vaw_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(vaw_ave_words$transition)),
                   labels=c("Carter to Reagan"="Carter to Reagan", "Reagan to G H W Bush"="*Reagan to G H W Bush", "G H W Bush to Clinton"="*G H W Bush to Clinton", "Clinton to G W Bush"="*Clinton to G W Bush", "G W Bush to Obama"="*G W Bush to Obama", "Obama to Trump"="*Obama to Trump")) +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
vaw<-vaw+ggtitle("Violence against Women") +theme(plot.title=element_text(hjust=0.5))
lgbti_ave_words<-join(lgbti_ave_words,info, by ="year", match="first")
lgbti_ave_words$transition<-factor(lgbti_ave_words$transition, levels=c("Carter to Reagan", "Reagan to G H W Bush", "G H W Bush to Clinton", "Clinton to G W Bush", "G W Bush to Obama", "Obama to Trump"))
lgbti<-ggplot(lgbti_ave_words[c(2,4,6,8,10,12),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(lgbti_ave_words$transition)),
                   labels=c("Reagan to G H W Bush"="Reagan to G H W Bush", "G H W Bush to Clinton"="G H W Bush to Clinton", "Clinton to G W Bush"="Clinton to G W Bush", "G W Bush to Obama"="*G W Bush to Obama", "Obama to Trump"="*Obama to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
lgbti<-lgbti+ggtitle("LGBTI+ Rights") +theme(plot.title=element_text(hjust=0.5))
figure3<-ggarrange(family, gender, sexual, vaw, lgbti, ncol=2, nrow=3, common.legend=TRUE, legend="bottom")
figure3

######################################################################################################################################
# Figure A1: Percentage change in average word count of State Department human rights reports for preceding same party administration
######################################################################################################################################

# Calculate total number of words per report year
ave_words_appx<-setNames(aggregate(reports$total.words, by=list(Category=reports$year), FUN=sum), c("year", "words"))

# Calculate number of reports per year
ave_words_appx$reports<-summary(as.factor(reports$year))

# Calculate average word count per report year
ave_words_appx<-ave_words_appx[c(3,4,1,6,5,8,7,10,9,12),]
ave_words_appx$ave.words<-ave_words_appx$words/ave_words_appx$reports

# Calculate difference between average word count for the first year of the new administration and the last year of the preceding same party administration
ave_words_appx$ave.diff<-NA
ave_words_appx$ave.diff[1:2]<-ave_words_appx[2,c(4)]-ave_words_appx[1,c(4)]
ave_words_appx$ave.diff[3:4]<-ave_words_appx[4,c(4)]-ave_words_appx[3,c(4)]
ave_words_appx$ave.diff[5:6]<-ave_words_appx[6,c(4)]-ave_words_appx[5,c(4)]
ave_words_appx$ave.diff[7:8]<-ave_words_appx[8,c(4)]-ave_words_appx[7,c(4)]
ave_words_appx$ave.diff[9:10]<-ave_words_appx[10,c(4)]-ave_words_appx[9,c(4)]

# Calculate percentage change in average word count for the first year of the new administration and the last year of the preceding same party administration
ave_words_appx$pct.change<-NA
ave_words_appx$pct.change[1:2]<-ave_words_appx[2,c(5)]/ave_words_appx[1,c(4)]*100
ave_words_appx$pct.change[3:4]<-ave_words_appx[4,c(5)]/ave_words_appx[3,c(4)]*100
ave_words_appx$pct.change[5:6]<-ave_words_appx[6,c(5)]/ave_words_appx[5,c(4)]*100
ave_words_appx$pct.change[7:8]<-ave_words_appx[8,c(5)]/ave_words_appx[7,c(4)]*100
ave_words_appx$pct.change[9:10]<-ave_words_appx[10,c(5)]/ave_words_appx[9,c(4)]*100

# Perfrom Welch two sample t-test to determine whether difference in word count from the first year of the new administration and the last year of the preceding same party administration is statistically significant at the 95% confidence level 
words_1988_1989<-reports[reports$year==1988 | reports$year==1989,] 
words_1980_1993<-reports[reports$year==1980 | reports$year==1993,] 
words_1992_2001<-reports[reports$year==1992 | reports$year==2001,] 
words_2000_2009<-reports[reports$year==2000 | reports$year==2009,]
words_2008_2017<-reports[reports$year==2008 | reports$year==2017,]
ave_words_appx$ttest.pvalue<-NA
ave_words_appx$ttest.pvalue[1:2]<-t.test(total.words~year, data=words_1988_1989)$p.value
ave_words_appx$ttest.pvalue[3:4]<-t.test(total.words~year, data=words_1980_1993)$p.value
ave_words_appx$ttest.pvalue[5:6]<-t.test(total.words~year, data=words_1992_2001)$p.value
ave_words_appx$ttest.pvalue[7:8]<-t.test(total.words~year, data=words_2000_2001)$p.value
ave_words_appx$ttest.pvalue[9:10]<-t.test(total.words~year, data=words_2008_2009)$p.value

# Produce graph
ave_words_appx<-join(ave_words_appx,info[,c(1,2)], by ="year", match="first")
ave_words_appx$transition<-NA
ave_words_appx$transition[1:2]<-"Reagan to G H W Bush"
ave_words_appx$transition[3:4]<-"Carter to Clinton"
ave_words_appx$transition[5:6]<-"G H W Bush to G W Bush"
ave_words_appx$transition[7:8]<-"Clinton to Obama"
ave_words_appx$transition[9:10]<-"G W Bush to Trump"
ave_words_appx$transition<-factor(ave_words_appx$transition, levels=c("Reagan to G H W Bush", "Carter to Clinton", "G H W Bush to G W Bush", "Clinton to Obama", "G W Bush to Trump"))
figurea1<-ggplot(ave_words_appx[c(2,4,6,8,10),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(ave_words_appx$transition)),
                   labels=c("Reagan to G H W Bush"="Reagan to G H W Bush", "Carter to Clinton"="*Carter to Clinton", "G H W Bush to G W Bush"="*G H W Bush to G W Bush", "Clinton to Obama"="Clinton to Obama", "G W Bush to Trump"="G W Bush to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
figurea1

###############################################################################################################################################
# Figure A2: Percentage change in section average word count of State Department human rights reports, for preceding same party administration
###############################################################################################################################################

# Calculate total number of words per section per report year
physical_words_appx<-reports[,c(5,11)] 
civil_words_appx<-reports[,c(5,12)] 
political_words_appx<-reports[,c(5,13)]
groups_words_appx<-reports[,c(5,14)] 
discrimination_words_appx<-reports[,c(5,15)]
worker_words_appx<-reports[,c(5,16)] 
physical_ave_words_appx<-setNames(aggregate(physical_words_appx$physical.words, by=list(Category=physical_words_appx$year), FUN=sum), c("year", "words"))
civil_ave_words_appx<-setNames(aggregate(civil_words_appx$civil.words, by=list(Category=civil_words_appx$year), FUN=sum), c("year", "words"))
political_ave_words_appx<-setNames(aggregate(political_words_appx$political.words, by=list(Category=political_words_appx$year), FUN=sum), c("year", "words"))
groups_ave_words_appx<-setNames(aggregate(groups_words_appx$groups.words, by=list(Category=groups_words_appx$year), FUN=sum), c("year", "words"))
discrimination_ave_words_appx<-setNames(aggregate(discrimination_words_appx$discrimination.words, by=list(Category=discrimination_words_appx$year), FUN=sum), c("year", "words"))
worker_ave_words_appx<-setNames(aggregate(worker_words_appx$worker.words, by=list(Category=worker_words_appx$year), FUN=sum), c("year", "words"))

# Calculate number of reports per section per year
physical_ave_words_appx$reports<-summary(as.factor(reports$year))
civil_ave_words_appx$reports<-summary(as.factor(reports$year))
political_ave_words_appx$reports<-summary(as.factor(reports$year))
groups_ave_words_appx$reports<-summary(as.factor(reports$year))
discrimination_ave_words_appx$reports<-summary(as.factor(reports$year))
worker_ave_words_appx$reports<-summary(as.factor(reports$year))

# Remove 1980 and 1981 from discrimination_ave_words_appx and worker_ave_words_appx as sections do not exist in reports
discrimination_ave_words_appx<-discrimination_ave_words_appx[!(discrimination_ave_words_appx$year==1980 | discrimination_ave_words_appx$year==1981),]
worker_ave_words_appx<-worker_ave_words_appx[!(worker_ave_words_appx$year==1980 | worker_ave_words_appx$year==1981),]

# Calculate average word count per section per report year
physical_ave_words_appx<-physical_ave_words_appx[c(3,4,1,6,5,8,7,10,9,12),]
civil_ave_words_appx<-civil_ave_words_appx[c(3,4,1,6,5,8,7,10,9,12),]
political_ave_words_appx<-political_ave_words_appx[c(3,4,1,6,5,8,7,10,9,12),]
groups_ave_words_appx<-groups_ave_words_appx[c(3,4,1,6,5,8,7,10,9,12),]
discrimination_ave_words_appx<-discrimination_ave_words_appx[c(1,2,3,6,5,8,7,10),]
worker_ave_words_appx<-worker_ave_words_appx[c(1,2,3,6,5,8,7,10),]
physical_ave_words_appx$ave.words<-physical_ave_words_appx$words/physical_ave_words_appx$reports
civil_ave_words_appx$ave.words<-civil_ave_words_appx$words/civil_ave_words_appx$reports
political_ave_words_appx$ave.words<-political_ave_words_appx$words/political_ave_words_appx$reports
groups_ave_words_appx$ave.words<-groups_ave_words_appx$words/groups_ave_words_appx$reports
discrimination_ave_words_appx$ave.words<-discrimination_ave_words_appx$words/discrimination_ave_words_appx$reports
worker_ave_words_appx$ave.words<-worker_ave_words_appx$words/worker_ave_words_appx$reports

# Calculate difference between average word count per section for the first year of the new administration and the last year of the preceding same party administration
physical_ave_words_appx$ave.diff<-NA
physical_ave_words_appx$ave.diff[1:2]<-physical_ave_words_appx[2,c(4)]-physical_ave_words_appx[1,c(4)]
physical_ave_words_appx$ave.diff[3:4]<-physical_ave_words_appx[4,c(4)]-physical_ave_words_appx[3,c(4)]
physical_ave_words_appx$ave.diff[5:6]<-physical_ave_words_appx[6,c(4)]-physical_ave_words_appx[5,c(4)]
physical_ave_words_appx$ave.diff[7:8]<-physical_ave_words_appx[8,c(4)]-physical_ave_words_appx[7,c(4)]
physical_ave_words_appx$ave.diff[9:10]<-physical_ave_words_appx[10,c(4)]-physical_ave_words_appx[9,c(4)]
civil_ave_words_appx$ave.diff<-NA
civil_ave_words_appx$ave.diff[1:2]<-civil_ave_words_appx[2,c(4)]-civil_ave_words_appx[1,c(4)]
civil_ave_words_appx$ave.diff[3:4]<-civil_ave_words_appx[4,c(4)]-civil_ave_words_appx[3,c(4)]
civil_ave_words_appx$ave.diff[5:6]<-civil_ave_words_appx[6,c(4)]-civil_ave_words_appx[5,c(4)]
civil_ave_words_appx$ave.diff[7:8]<-civil_ave_words_appx[8,c(4)]-civil_ave_words_appx[7,c(4)]
civil_ave_words_appx$ave.diff[9:10]<-civil_ave_words_appx[10,c(4)]-civil_ave_words_appx[9,c(4)]
political_ave_words_appx$ave.diff<-NA
political_ave_words_appx$ave.diff[1:2]<-political_ave_words_appx[2,c(4)]-political_ave_words_appx[1,c(4)]
political_ave_words_appx$ave.diff[3:4]<-political_ave_words_appx[4,c(4)]-political_ave_words_appx[3,c(4)]
political_ave_words_appx$ave.diff[5:6]<-political_ave_words_appx[6,c(4)]-political_ave_words_appx[5,c(4)]
political_ave_words_appx$ave.diff[7:8]<-political_ave_words_appx[8,c(4)]-political_ave_words_appx[7,c(4)]
political_ave_words_appx$ave.diff[9:10]<-political_ave_words_appx[10,c(4)]-political_ave_words_appx[9,c(4)]
groups_ave_words_appx$ave.diff<-NA
groups_ave_words_appx$ave.diff[1:2]<-groups_ave_words_appx[2,c(4)]-groups_ave_words_appx[1,c(4)]
groups_ave_words_appx$ave.diff[3:4]<-groups_ave_words_appx[4,c(4)]-groups_ave_words_appx[3,c(4)]
groups_ave_words_appx$ave.diff[5:6]<-groups_ave_words_appx[6,c(4)]-groups_ave_words_appx[5,c(4)]
groups_ave_words_appx$ave.diff[7:8]<-groups_ave_words_appx[8,c(4)]-groups_ave_words_appx[7,c(4)]
groups_ave_words_appx$ave.diff[9:10]<-groups_ave_words_appx[10,c(4)]-groups_ave_words_appx[9,c(4)]
discrimination_ave_words_appx$ave.diff<-NA
discrimination_ave_words_appx$ave.diff[1:2]<-discrimination_ave_words_appx[2,c(4)]-discrimination_ave_words_appx[1,c(4)]
discrimination_ave_words_appx$ave.diff[3:4]<-discrimination_ave_words_appx[4,c(4)]-discrimination_ave_words_appx[3,c(4)]
discrimination_ave_words_appx$ave.diff[5:6]<-discrimination_ave_words_appx[6,c(4)]-discrimination_ave_words_appx[5,c(4)]
discrimination_ave_words_appx$ave.diff[7:8]<-discrimination_ave_words_appx[8,c(4)]-discrimination_ave_words_appx[7,c(4)]
worker_ave_words_appx$ave.diff<-NA
worker_ave_words_appx$ave.diff[1:2]<-worker_ave_words_appx[2,c(4)]-worker_ave_words_appx[1,c(4)]
worker_ave_words_appx$ave.diff[3:4]<-worker_ave_words_appx[4,c(4)]-worker_ave_words_appx[3,c(4)]
worker_ave_words_appx$ave.diff[5:6]<-worker_ave_words_appx[6,c(4)]-worker_ave_words_appx[5,c(4)]
worker_ave_words_appx$ave.diff[7:8]<-worker_ave_words_appx[8,c(4)]-worker_ave_words_appx[7,c(4)]

# Calculate percentage change in average word count per section for the first year of the new administration and the last year of the preceding same party administration
physical_ave_words_appx$pct.change<-NA
physical_ave_words_appx$pct.change[1:2]<-physical_ave_words_appx[2,c(5)]/physical_ave_words_appx[1,c(4)]*100
physical_ave_words_appx$pct.change[3:4]<-physical_ave_words_appx[4,c(5)]/physical_ave_words_appx[3,c(4)]*100
physical_ave_words_appx$pct.change[5:6]<-physical_ave_words_appx[6,c(5)]/physical_ave_words_appx[5,c(4)]*100
physical_ave_words_appx$pct.change[7:8]<-physical_ave_words_appx[8,c(5)]/physical_ave_words_appx[7,c(4)]*100
physical_ave_words_appx$pct.change[9:10]<-physical_ave_words_appx[10,c(5)]/physical_ave_words_appx[9,c(4)]*100
civil_ave_words_appx$pct.change<-NA
civil_ave_words_appx$pct.change[1:2]<-civil_ave_words_appx[2,c(5)]/civil_ave_words_appx[1,c(4)]*100
civil_ave_words_appx$pct.change[3:4]<-civil_ave_words_appx[4,c(5)]/civil_ave_words_appx[3,c(4)]*100
civil_ave_words_appx$pct.change[5:6]<-civil_ave_words_appx[6,c(5)]/civil_ave_words_appx[5,c(4)]*100
civil_ave_words_appx$pct.change[7:8]<-civil_ave_words_appx[8,c(5)]/civil_ave_words_appx[7,c(4)]*100
civil_ave_words_appx$pct.change[9:10]<-civil_ave_words_appx[10,c(5)]/civil_ave_words_appx[9,c(4)]*100
political_ave_words_appx$pct.change<-NA
political_ave_words_appx$pct.change[1:2]<-political_ave_words_appx[2,c(5)]/political_ave_words_appx[1,c(4)]*100
political_ave_words_appx$pct.change[3:4]<-political_ave_words_appx[4,c(5)]/political_ave_words_appx[3,c(4)]*100
political_ave_words_appx$pct.change[5:6]<-political_ave_words_appx[6,c(5)]/political_ave_words_appx[5,c(4)]*100
political_ave_words_appx$pct.change[7:8]<-political_ave_words_appx[8,c(5)]/political_ave_words_appx[7,c(4)]*100
political_ave_words_appx$pct.change[9:10]<-political_ave_words_appx[10,c(5)]/political_ave_words_appx[9,c(4)]*100
groups_ave_words_appx$pct.change<-NA
groups_ave_words_appx$pct.change[1:2]<-groups_ave_words_appx[2,c(5)]/groups_ave_words_appx[1,c(4)]*100
groups_ave_words_appx$pct.change[3:4]<-groups_ave_words_appx[4,c(5)]/groups_ave_words_appx[3,c(4)]*100
groups_ave_words_appx$pct.change[5:6]<-groups_ave_words_appx[6,c(5)]/groups_ave_words_appx[5,c(4)]*100
groups_ave_words_appx$pct.change[7:8]<-groups_ave_words_appx[8,c(5)]/groups_ave_words_appx[7,c(4)]*100
groups_ave_words_appx$pct.change[9:10]<-groups_ave_words_appx[10,c(5)]/groups_ave_words_appx[9,c(4)]*100
discrimination_ave_words_appx$pct.change<-NA
discrimination_ave_words_appx$pct.change[1:2]<-discrimination_ave_words_appx[2,c(5)]/discrimination_ave_words_appx[1,c(4)]*100
discrimination_ave_words_appx$pct.change[3:4]<-discrimination_ave_words_appx[4,c(5)]/discrimination_ave_words_appx[3,c(4)]*100
discrimination_ave_words_appx$pct.change[5:6]<-discrimination_ave_words_appx[6,c(5)]/discrimination_ave_words_appx[5,c(4)]*100
discrimination_ave_words_appx$pct.change[7:8]<-discrimination_ave_words_appx[8,c(5)]/discrimination_ave_words_appx[7,c(4)]*100
worker_ave_words_appx$pct.change<-NA
worker_ave_words_appx$pct.change[1:2]<-worker_ave_words_appx[2,c(5)]/worker_ave_words_appx[1,c(4)]*100
worker_ave_words_appx$pct.change[3:4]<-worker_ave_words_appx[4,c(5)]/worker_ave_words_appx[3,c(4)]*100
worker_ave_words_appx$pct.change[5:6]<-worker_ave_words_appx[6,c(5)]/worker_ave_words_appx[5,c(4)]*100
worker_ave_words_appx$pct.change[7:8]<-worker_ave_words_appx[8,c(5)]/worker_ave_words_appx[7,c(4)]*100

# Perfrom Welch two sample t-test to determine whether difference in section word count from the first year of the new administration and the last year of the preceding same party administration is statistically significant at the 95% confidence level 
physical_ave_words_appx_1988_1989<-physical_words_appx[physical_words_appx$year==1988 | physical_words_appx$year==1989,] 
physical_ave_words_appx_1980_1993<-physical_words_appx[physical_words_appx$year==1980 | physical_words_appx$year==1993,] 
physical_ave_words_appx_1992_2001<-physical_words_appx[physical_words_appx$year==1992 | physical_words_appx$year==2001,]
physical_ave_words_appx_2000_2009<-physical_words_appx[physical_words_appx$year==2000 | physical_words_appx$year==2009,]
physical_ave_words_appx_2008_2017<-physical_words_appx[physical_words_appx$year==2008 | physical_words_appx$year==2017,]
physical_ave_words_appx$ttest.pvalue<-NA
physical_ave_words_appx$ttest.pvalue[1:2]<-t.test(physical.words~year, data=physical_ave_words_appx_1988_1989)$p.value
physical_ave_words_appx$ttest.pvalue[3:4]<-t.test(physical.words~year, data=physical_ave_words_appx_1980_1993)$p.value
physical_ave_words_appx$ttest.pvalue[5:6]<-t.test(physical.words~year, data=physical_ave_words_appx_1992_2001)$p.value
physical_ave_words_appx$ttest.pvalue[7:8]<-t.test(physical.words~year, data=physical_ave_words_appx_2000_2009)$p.value
physical_ave_words_appx$ttest.pvalue[9:10]<-t.test(physical.words~year, data=physical_ave_words_appx_2008_2017)$p.value
civil_ave_words_appx_1988_1989<-civil_words_appx[civil_words_appx$year==1988 | civil_words_appx$year==1989,] 
civil_ave_words_appx_1980_1993<-civil_words_appx[civil_words_appx$year==1980 | civil_words_appx$year==1993,] 
civil_ave_words_appx_1992_2001<-civil_words_appx[civil_words_appx$year==1992 | civil_words_appx$year==2001,]
civil_ave_words_appx_2000_2009<-civil_words_appx[civil_words_appx$year==2000 | civil_words_appx$year==2009,]
civil_ave_words_appx_2008_2017<-civil_words_appx[civil_words_appx$year==2008 | civil_words_appx$year==2017,]
civil_ave_words_appx$ttest.pvalue<-NA
civil_ave_words_appx$ttest.pvalue[1:2]<-t.test(civil.words~year, data=civil_ave_words_appx_1988_1989)$p.value
civil_ave_words_appx$ttest.pvalue[3:4]<-t.test(civil.words~year, data=civil_ave_words_appx_1980_1993)$p.value
civil_ave_words_appx$ttest.pvalue[5:6]<-t.test(civil.words~year, data=civil_ave_words_appx_1992_2001)$p.value
civil_ave_words_appx$ttest.pvalue[7:8]<-t.test(civil.words~year, data=civil_ave_words_appx_2000_2009)$p.value
civil_ave_words_appx$ttest.pvalue[9:10]<-t.test(civil.words~year, data=civil_ave_words_appx_2008_2017)$p.value
political_ave_words_appx_1988_1989<-political_words_appx[political_words_appx$year==1988 | political_words_appx$year==1989,] 
political_ave_words_appx_1980_1993<-political_words_appx[political_words_appx$year==1980 | political_words_appx$year==1993,] 
political_ave_words_appx_1992_2001<-political_words_appx[political_words_appx$year==1992 | political_words_appx$year==2001,]
political_ave_words_appx_2000_2009<-political_words_appx[political_words_appx$year==2000 | political_words_appx$year==2009,]
political_ave_words_appx_2008_2017<-political_words_appx[political_words_appx$year==2008 | political_words_appx$year==2017,]
political_ave_words_appx$ttest.pvalue<-NA
political_ave_words_appx$ttest.pvalue[1:2]<-t.test(political.words~year, data=political_ave_words_appx_1988_1989)$p.value
political_ave_words_appx$ttest.pvalue[3:4]<-t.test(political.words~year, data=political_ave_words_appx_1980_1993)$p.value
political_ave_words_appx$ttest.pvalue[5:6]<-t.test(political.words~year, data=political_ave_words_appx_1992_2001)$p.value
political_ave_words_appx$ttest.pvalue[7:8]<-t.test(political.words~year, data=political_ave_words_appx_2000_2009)$p.value
political_ave_words_appx$ttest.pvalue[9:10]<-t.test(political.words~year, data=political_ave_words_appx_2008_2017)$p.value
groups_ave_words_appx_1988_1989<-groups_words_appx[groups_words_appx$year==1988 | groups_words_appx$year==1989,] 
groups_ave_words_appx_1980_1993<-groups_words_appx[groups_words_appx$year==1980 | groups_words_appx$year==1993,] 
groups_ave_words_appx_1992_2001<-groups_words_appx[groups_words_appx$year==1992 | groups_words_appx$year==2001,]
groups_ave_words_appx_2000_2009<-groups_words_appx[groups_words_appx$year==2000 | groups_words_appx$year==2009,]
groups_ave_words_appx_2008_2017<-groups_words_appx[groups_words_appx$year==2008 | groups_words_appx$year==2017,]
groups_ave_words_appx$ttest.pvalue<-NA
groups_ave_words_appx$ttest.pvalue[1:2]<-t.test(groups.words~year, data=groups_ave_words_appx_1988_1989)$p.value
groups_ave_words_appx$ttest.pvalue[3:4]<-t.test(groups.words~year, data=groups_ave_words_appx_1980_1993)$p.value
groups_ave_words_appx$ttest.pvalue[5:6]<-t.test(groups.words~year, data=groups_ave_words_appx_1992_2001)$p.value
groups_ave_words_appx$ttest.pvalue[7:8]<-t.test(groups.words~year, data=groups_ave_words_appx_2000_2009)$p.value
groups_ave_words_appx$ttest.pvalue[9:10]<-t.test(groups.words~year, data=groups_ave_words_appx_2008_2017)$p.value
discrimination_ave_words_appx_1988_1989<-discrimination_words_appx[discrimination_words_appx$year==1988 | discrimination_words_appx$year==1989,] 
discrimination_ave_words_appx_1992_2001<-discrimination_words_appx[discrimination_words_appx$year==1992 | discrimination_words_appx$year==2001,]
discrimination_ave_words_appx_2000_2009<-discrimination_words_appx[discrimination_words_appx$year==2000 | discrimination_words_appx$year==2009,]
discrimination_ave_words_appx_2008_2017<-discrimination_words_appx[discrimination_words_appx$year==2008 | discrimination_words_appx$year==2017,]
discrimination_ave_words_appx$ttest.pvalue<-NA
discrimination_ave_words_appx$ttest.pvalue[1:2]<-t.test(discrimination.words~year, data=discrimination_ave_words_appx_1988_1989)$p.value
discrimination_ave_words_appx$ttest.pvalue[3:4]<-t.test(discrimination.words~year, data=discrimination_ave_words_appx_1992_2001)$p.value
discrimination_ave_words_appx$ttest.pvalue[5:6]<-t.test(discrimination.words~year, data=discrimination_ave_words_appx_2000_2009)$p.value
discrimination_ave_words_appx$ttest.pvalue[7:8]<-t.test(discrimination.words~year, data=discrimination_ave_words_appx_2008_2017)$p.value
worker_ave_words_appx_1988_1989<-worker_words_appx[worker_words_appx$year==1988 | worker_words_appx$year==1989,] 
worker_ave_words_appx_1992_2001<-worker_words_appx[worker_words_appx$year==1992 | worker_words_appx$year==2001,]
worker_ave_words_appx_2000_2009<-worker_words_appx[worker_words_appx$year==2000 | worker_words_appx$year==2009,]
worker_ave_words_appx_2008_2017<-worker_words_appx[worker_words_appx$year==2008 | worker_words_appx$year==2017,]
worker_ave_words_appx$ttest.pvalue<-NA
worker_ave_words_appx$ttest.pvalue[1:2]<-t.test(worker.words~year, data=worker_ave_words_appx_1988_1989)$p.value
worker_ave_words_appx$ttest.pvalue[3:4]<-t.test(worker.words~year, data=worker_ave_words_appx_1992_2001)$p.value
worker_ave_words_appx$ttest.pvalue[5:6]<-t.test(worker.words~year, data=worker_ave_words_appx_2000_2009)$p.value
worker_ave_words_appx$ttest.pvalue[7:8]<-t.test(worker.words~year, data=worker_ave_words_appx_2008_2017)$p.value

# Produce graph
physical_ave_words_appx<-join(physical_ave_words_appx,ave_words_appx[,c(1,8,9)], by ="year", match="first")
physical_ave_words_appx$transition<-factor(physical_ave_words_appx$transition, levels=c("Reagan to G H W Bush", "Carter to Clinton", "G H W Bush to G W Bush", "Clinton to Obama", "G W Bush to Trump"))
physical_appx<-ggplot(physical_ave_words_appx[c(2,4,6,8,10),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(physical_ave_words_appx$transition)),
                   labels=c("Reagan to G H W Bush"="Reagan to G H W Bush", "Carter to Clinton"="*Carter to Clinton", "G H W Bush to G W Bush"="*G H W Bush to G W Bush", "Clinton to Obama"="Clinton to Obama", "G W Bush to Trump"="G W Bush to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
physical_appx<-physical_appx+ggtitle("Physical Integrity Rights") +theme(plot.title=element_text(hjust=0.5))
civil_ave_words_appx<-join(civil_ave_words_appx,ave_words_appx[,c(1,8,9)], by ="year", match="first")
civil_ave_words_appx$transition<-factor(civil_ave_words_appx$transition, levels=c("Reagan to G H W Bush", "Carter to Clinton", "G H W Bush to G W Bush", "Clinton to Obama", "G W Bush to Trump"))
civil_appx<-ggplot(civil_ave_words_appx[c(2,4,6,8,10),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(civil_ave_words_appx$transition)),
                   labels=c("Reagan to G H W Bush"="Reagan to G H W Bush", "Carter to Clinton"="*Carter to Clinton", "G H W Bush to G W Bush"="*G H W Bush to G W Bush", "Clinton to Obama"="Clinton to Obama", "G W Bush to Trump"="G W Bush to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
civil_appx<-civil_appx+ggtitle("Civil Liberties") +theme(plot.title=element_text(hjust=0.5))
political_ave_words_appx<-join(political_ave_words_appx,ave_words_appx[,c(1,8,9)], by ="year", match="first")
political_ave_words_appx$transition<-factor(political_ave_words_appx$transition, levels=c("Reagan to G H W Bush", "Carter to Clinton", "G H W Bush to G W Bush", "Clinton to Obama", "G W Bush to Trump"))
political_appx<-ggplot(political_ave_words_appx[c(2,4,6,8,10),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(political_ave_words_appx$transition)),
                   labels=c("Reagan to G H W Bush"="*Reagan to G H W Bush", "Carter to Clinton"="*Carter to Clinton", "G H W Bush to G W Bush"="*G H W Bush to G W Bush", "Clinton to Obama"="*Clinton to Obama", "G W Bush to Trump"="G W Bush to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
political_appx<-political_appx+ggtitle("Political Rights") +theme(plot.title=element_text(hjust=0.5))
groups_ave_words_appx<-join(groups_ave_words_appx,ave_words_appx[,c(1,8,9)], by ="year", match="first")
groups_ave_words_appx$transition<-factor(groups_ave_words_appx$transition, levels=c("Reagan to G H W Bush", "Carter to Clinton", "G H W Bush to G W Bush", "Clinton to Obama", "G W Bush to Trump"))
groups_appx<-ggplot(groups_ave_words_appx[c(2,4,6,8,10),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(groups_ave_words_appx$transition)),
                   labels=c("Reagan to G H W Bush"="Reagan to G H W Bush", "Carter to Clinton"="Carter to Clinton", "G H W Bush to G W Bush"="*G H W Bush to G W Bush", "Clinton to Obama"="Clinton to Obama", "G W Bush to Trump"="G W Bush to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
groups_appx<-groups_appx+ggtitle("Domestic and International Human Rights Groups") +theme(plot.title=element_text(hjust=0.5))
discrimination_ave_words_appx<-join(discrimination_ave_words_appx,ave_words_appx[,c(1,8,9)], by ="year", match="first")
discrimination_ave_words_appx$transition<-factor(discrimination_ave_words_appx$transition, levels=c("Reagan to G H W Bush", "G H W Bush to G W Bush", "Clinton to Obama", "G W Bush to Trump"))
discrimination_appx<-ggplot(discrimination_ave_words_appx[c(2,4,6,8),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(discrimination_ave_words_appx$transition)),
                   labels=c("Reagan to G H W Bush"="*Reagan to G H W Bush", "G H W Bush to G W Bush"="*G H W Bush to G W Bush", "Clinton to Obama"="*Clinton to Obama", "G W Bush to Trump"="*G W Bush to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
discrimination_appx<-discrimination_appx+ggtitle("Discrimination and Societal Abuses") +theme(plot.title=element_text(hjust=0.5))
worker_ave_words_appx<-join(worker_ave_words_appx,ave_words_appx[,c(1,8,9)], by ="year", match="first")
worker_ave_words_appx$transition<-factor(worker_ave_words_appx$transition, levels=c("Reagan to G H W Bush", "G H W Bush to G W Bush", "Clinton to Obama", "G W Bush to Trump"))
worker_appx<-ggplot(worker_ave_words_appx[c(2,4,6,8),], aes(x=transition, y=pct.change)) + 
  geom_bar(stat='identity', aes(fill=party), width=.5, position="dodge")  +
  scale_fill_manual(name="", 
                    labels=c("Democrat Transition", "Republican Transition"), 
                    values=c("Democrat"="grey70", "Republican"="grey0")) +
  labs(x="", y="") + 
  theme_linedraw() +
  theme(axis.line=element_line(colour="black"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.position="bottom",
        plot.caption=element_text(hjust=0.5)) +
  scale_x_discrete(limits=rev(levels(worker_ave_words_appx$transition)),
                   labels=c("Reagan to G H W Bush"="Reagan to G H W Bush", "G H W Bush to G W Bush"="*G H W Bush to G W Bush", "Clinton to Obama"="Clinton to Obama", "G W Bush to Trump"="*G W Bush to Trump"))   +
  geom_hline(aes(yintercept=0), linetype="dashed") + coord_flip()
worker_appx<-worker_appx+ggtitle("Worker Rights") +theme(plot.title=element_text(hjust=0.5))
figurea2<-ggarrange(physical_appx, civil_appx, political_appx, groups_appx, discrimination_appx, worker_appx, ncol=2, nrow=3, common.legend=TRUE, legend="bottom")
figurea2